package week13.queue.memory;

import week13.queue.api.ConsumerGroup;
import week13.queue.api.Offset;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MemoryOffset implements Offset {
    Map<String, Long> offsetMap = new ConcurrentHashMap<>();

    @Override
    public void save(ConsumerGroup consumerGroup, long offset) {
        offsetMap.put(consumerGroup.getConsumerGroupName(), offset);
    }

    @Override
    public long read(ConsumerGroup consumerGroup) {
        return offsetMap.computeIfAbsent(consumerGroup.getConsumerGroupName(), k -> 0L);
    }
}
